<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: viewtopic.php Sat Jul 18 12:38:30 CST 2009
 */

include "conf.inc";

$tid = isset($_GET["tid"]) ? intval($_GET["tid"]) : 0 ;
$start = isset($_GET["start"]) ? intval($_GET["start"]) : 0 ;

//get this topic 
$group_handler = xoops_getmodulehandler("group");
$threads_handler = xoops_getmodulehandler("threads");
$post_handler = xoops_getmodulehandler("posts");

$threads_obj = $threads_handler->get($tid);
$group_obj = $group_handler->getGroup($threads_obj->getVar("group_id"));

$xoopsOption["xoops_pagetitle"] = $xoopsModuleConfig["groupstitle"] . $group_obj->getVar("group_name") . " - " . $threads_obj->getVar("threads_subject");
$xoopsOption["template_main"] = "groups_group_viewtopic.html";

require "group_header.php";

$limit = 10;
$extra = "tid={$tid}";
$params = array(
    "criteria" => array(
        "tid"=>$tid,
        "post_status"=>0
    ),
    "start" => $start,
    "limit" => $limit,
    "sort"  => array("post_first"=>"DESC","post_id"=>"ASC"),
    "order" => "ASC",
    "extra" => $extra
);
if ( isset($_GET["go"]) && $_GET["go"] == "last" ) {
	$counts = (int)$post_handler->getPostCount($params);
	$params["start"] = (ceil($counts / $limit)-1) * $limit ;
}
$objs = $post_handler->getPosts($params);
$pagenav = $post_handler->getPageNav($params);

$user_ids = array();
if ( !empty($objs) ) {
    foreach ( $objs as $k=>$obj ) {
        $user_ids[] = $obj->getVar("uid");
    }
    $user_ids = array_unique($user_ids);
}

if ( $user_ids ) {
    $users = xoGroups::getUsers($user_ids);
}

$topics = array();
if ( !empty($objs) ) {
    foreach ( $objs as $k=>$obj ) {
		$topics [$k] = $obj->getValues();
		$topics[$k]["author_nickname"] = $obj->getVar("uid") ? $users[$obj->getVar("uid")]["name"] : "";
		$topics[$k]["author_avatar"] = $obj->getVar("uid") ? $users[$obj->getVar("uid")]["avatar"] : "";
		$topics[$k]["author_online"] = $obj->getVar("uid") ? $users[$obj->getVar("uid")]["online"] : "";
		$topics[$k]["create_time"] = $obj->createTime();
    }
}
$group_id = $group_obj->id();
include_once(dirname(__FILE__)."/include/form.quick.post.php");
$xoopsTpl->assign(array("group"=>$group_obj->info(),"topics"=>$topics,"pagenav"=>$pagenav));
include "footer.php";
?>
